package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.query.CarPackageAuditQuery;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import cn.wolfcode.car.shiro.ShiroUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/business/carPackageAudit")
public class CarPackageAuditController {
    private static final String prefix = "business/carPackageAudit/";

    @Resource
    private ICarPackageAuditService carPackageAuditService;

    @RequiresPermissions("business:carPackageAudit:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("business:carPackageAudit:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> query(CarPackageAuditQuery qo) {
        return carPackageAuditService.query(qo);
    }

    @RequiresPermissions("business:carPackageAudit:deploy")
    @RequestMapping("/deployPage")
    public String deployPage() {
        return prefix + "deploy";
    }

    // 删除
    @RequiresPermissions("business:carPackageAudit:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(@ModelAttribute("ids") Long id) {
        carPackageAuditService.deleteById(id);
        return AjaxResult.success();
    }

    // 查看进度
    @RequestMapping("/processImg")
    public void getProcessImageByAuditId(Long id, HttpServletResponse response) throws IOException {
        IOUtils.copy(carPackageAuditService.getProcessImageByAuditId(id), response.getOutputStream());
    }

    @RequiresPermissions("business:carPackageAudit:cancelApply")
    @RequestMapping("/cancelApply")
    @ResponseBody
    public AjaxResult cancelApply(Long id) {
        carPackageAuditService.cancelApply(id);
        return AjaxResult.success();
    }

    // 我的待办
    @RequestMapping("/todoPage")
    public String todoPage(){
        return prefix + "todoPage";
    }

    @RequestMapping("/todoQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> todoQuery(CarPackageAuditQuery qo){
        qo.setAuditorId(ShiroUtils.getUser().getId());
        qo.setStatus(CarPackageAudit.STATUS_IN_ROGRESS);
        return carPackageAuditService.query(qo);
    }

    // 审核
    @RequestMapping("/auditPage")
    public String auditPage(Long id, Model model){
        model.addAttribute("id", id);
        return prefix + "audit";
    }

    @RequestMapping("/audit")
    @ResponseBody
    public AjaxResult audit(Long id, Integer auditStatus, String info){
        carPackageAuditService.audit(id, auditStatus, info);
        return AjaxResult.success();
    }

    // 已办
    @RequestMapping("/donePage")
    public String donePage(){
        return prefix + "donePage";
    }

    @RequestMapping("/doneQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> doneQuery(CarPackageAuditQuery qo){
        qo.setAuditorId(ShiroUtils.getUser().getId());
        qo.setAuditors(CarPackageAudit.AUDITORS_ALREADY);
        return carPackageAuditService.query(qo);
    }
}


